package org.example.quartz.task;

import cn.hutool.db.Entity;
import cn.hutool.db.Session;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.example.quartz.service.LogService;
import org.example.quartz.service.OrgService;
import org.example.quartz.service.UtilService;
import org.example.quartz.util.DataSourceUtil;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
// 再Job被执行结束后，将会更新JobDataMap，这样下次Job执行后悔使用新的值而不是初始值
@PersistJobDataAfterExecution
// 同一时间将只有一个Job实例被执行，为了避免并发问题导致数据紊乱，建议这两个注解一起使用
@DisallowConcurrentExecution
public class UserQuartz implements QuartzTaskJob {
    /**
     * 用户接口
     * 接口地址：GSCloud文档3.10
     * http://ip:port/api/jg/syncbddata/v1.0/bdsyncinterface/syncUser
     **/
    @SneakyThrows
    @Override
    public void execute(JobExecutionContext context) {

        //全局接口配置
        String IP = DataSourceUtil.IP;
        String PORT = DataSourceUtil.PORT;
        String ECC = DataSourceUtil.ECC;
        String Bearer = DataSourceUtil.Bearer;

        //日志
        LogService loginfo = SpringUtil.getBean(LogService.class);
        //orgService
        OrgService orgService = SpringUtil.getBean(OrgService.class);
        //orgService
        UtilService utilService = SpringUtil.getBean(UtilService.class);

        //传入变量 lastTime 最后抓取时间
        //链接BIP数据库，查询出需要同步的数据，根据TS时间进行判断。
        //TS从中间库表获取到最后一次更新时间，根据此时间进行增量查询推送；
        //中间库日志表记录推送日志，成功、失败、返回信息等（备查）
        //中间库记录最后推送数据的时间，根据时间进行下次查询的条件；
        //查询脚本
        /**
         *SELECT
         * US.USER_CODE AS CODE,
         * US.USER_NAME AS NAME,
         * ORG.CODE AS ORGCODE,
         * US.TELPHONE AS CONTACTINFO,
         * PSN.EMAIL AS EMAIL,
         * PSN.ID AS IDNUMBER,
         * PSN.SEX AS SEX,
         * 'S' AS SECLEVEL,
         * US.USER_NOTE AS REMARK
         * FROM
         * SM_USER US
         * LEFT JOIN ORG_ORGS ORG ON US.PK_ORG = ORG.PK_ORG
         * LEFT JOIN BD_PSNDOC PSN ON US.PK_PSNDOC = PSN.PK_PSNDOC
         * WHERE
         * US.DR =0
         * AND
         * US.ENABLESTATE = 2
         * AND
         * ORG.CODE IS NOT NULL
         * AND
         * ORG.CODE !='GLOBAL'
         * AND
         * US.TS >'2023-12-31 00:00:00'
         * */
        //查询脚本
        String methodName = "UserQuartz";
//        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        JdbcTemplate jdbcTemplate = SpringUtil.getBean(JdbcTemplate.class);
        //1、查询时间
        String ts = utilService.getTs(methodName, jdbcTemplate);
        String orgStr = utilService.getGSOrgStr(jdbcTemplate);
        //2、根据时间查询数据集 (停用和删除的过滤掉，暂不考虑，线下处理)
        String selectSQL = "SELECT \n" +
                "US.USER_CODE AS CODE,\n" +
                "US.USER_NAME AS NAME,\n" +
                "ORG.CODE AS ORGCODE,\n" +
                "US.TELPHONE AS CONTACTINFO,\n" +
                "PSN.EMAIL AS EMAIL,\n" +
                "PSN.ID AS IDNUMBER,\n" +
                "PSN.SEX AS SEX,\n" +
                "' ' AS SECLEVEL,\n" +
                "US.TS AS TS,\n" +
                "US.USER_NOTE AS REMARK\n" +
                "FROM \n" +
                "SM_USER US\n" +
                "LEFT JOIN ORG_ORGS ORG ON US.PK_ORG = ORG.PK_ORG\n" +
                "LEFT JOIN BD_PSNDOC PSN ON US.PK_PSNDOC = PSN.PK_PSNDOC\n" +
                "WHERE \n" +
                "US.DR =0\n" +
                "AND \n" +
                "US.ENABLESTATE = 2\n" +
//                "AND\n" +
//                "ORG.CODE IS NOT NULL\n" +
//                "AND \n" +
//                "ORG.CODE !='GLOBAL'\n" +
                "AND \n" +
                "ORG.CODE IN ("+orgStr+")\n" +
                "AND\n" +
                "US.TS >'"+ts+"'\n" +
                "ORDER BY TS ASC";
        //查询数据，根据数据结果推送给GS浪潮
        Session oracleSession = DataSourceUtil.getOracleSession();
        List<Entity> query = oracleSession.query(selectSQL);
        //拼装数据，推送给浪潮
        if(query.size()>0){
            for (Map<String, Object> gSmap : query) {

                //更换gsCode
                String gsCode = orgService.selectGsCode((String) gSmap.get("ORGCODE"),jdbcTemplate);
                /**
                 * GS规则：系统用户接口，用户编号需要保持集团内唯一，传递是需要在原人员编号前拼接上单位编号，保证集团内唯一
                 *
                 * */
                String GsJson =
                        "{\n" +
                                "    \"CODE\":\""+gsCode+gSmap.get("CODE")+"\",\n" +
                                "    \"NAME\":\""+gSmap.get("NAME")+"\",\n" +
                                "    \"ORGCODE\":\""+gsCode+"\",\n" +
                                "    \"CONTACTINFO\":\""+gSmap.get("CONTACTINFO")+"\",\n" +
                                "    \"EMAIL\":\""+gSmap.get("EMAIL")+"\",\n" +
                                "    \"IDNUMBER\":\""+gSmap.get("IDNUMBER")+"\",\n" +
                                "    \"SEX\":\""+gSmap.get("SEX")+"\",\n" +
                                "    \"SECLEVEL\":\""+gSmap.get("SECLEVEL")+"\",\n" +
                                "    \"REMARK\":\""+gSmap.get("REMARK")+"\"\n" +
                                "}";
                GsJson = GsJson.replace("null","");
                //调用GS接口，推送给GS浪潮，获取返回值;
                //1、推送用户档案
                String gsUrl = IP+":"+PORT+"/api/jg/syncbddata/v1.0/bdsyncinterface/syncUser";
                String returnMSG =   HttpRequest.post(gsUrl)
                        .header("Content-Type", "application/json")
                        .header("Accept-Language", "zh-CHS")
                        .header("X-ECC-Current-Tenant", ECC)
                        .header("Authorization", Bearer).timeout(200000)
                        .body(GsJson).execute().body();
                if(!returnMSG.isEmpty()){
                    String logInsert = "";
                    JSONObject returnObj = JSONObject.parseObject(returnMSG);
                    //成功
                    if(returnObj.get("code").equals("0") ){
                        int loginsert = loginfo.loginfo(jdbcTemplate,methodName,LocalDateTime.now().toString(),"成功",GsJson,returnMSG,"","",gsUrl, (String) gSmap.get("TS"));
                        if(loginsert>0){
                          //  System.out.println("日志写入成功！！");
                        }else{
                            System.out.println("日志写入失败！！");
                        }
                        //失败
                    }else if(returnObj.get("code").equals("1") ){
                        //写入日志表
                        int loginsert = loginfo.loginfo(jdbcTemplate,methodName,LocalDateTime.now().toString(),"失败",GsJson,returnMSG,"","",gsUrl, (String) gSmap.get("TS"));
                        if(loginsert>0){
                            //System.out.println("日志写入成功！！");
                        }else{
                            System.out.println("日志写入失败！！");
                        }
                    }else if(returnObj.get("code")==null && ("").equals(returnObj.get("code"))){
                        //写入日志表
                        int loginsert = loginfo.loginfo(jdbcTemplate, methodName, LocalDateTime.now().toString(), "数据返回异常", GsJson, returnMSG, "",  (String) gSmap.get("ID"), gsUrl, (String) gSmap.get("TS"));
                        if (loginsert > 0) {
                            System.out.println("日志写入成功！！");
                        } else {
                            System.out.println("日志写入失败！！");
                        }
                    }
                }
            }
        }else{
            System.out.println("暂无新数据推送");
        }

    }




    }

